{% if release == 'newton' %}

for i in $(neutron floatingip-list -c id -f value)
do
    neutron floatingip-disassociate $i
    neutron floatingip-delete $i
done
for i in $(neutron router-list -c id -f value); do neutron router-gateway-clear $i; done
for r in $(neutron router-list -c id -f value); do
    for p in $(neutron router-port-list $r -c id -f value); do
        neutron router-interface-delete $r port=$p || true
    done
done
for i in $(neutron router-list -c id -f value); do neutron router-delete $i; done
for i in $(neutron port-list -c id -f value); do neutron port-delete $i; done
for i in $(neutron net-list -c id -f value); do neutron net-delete $i; done

neutron net-create {{ public_net_name }} --router:external=True \
{% if public_physical_network != '' %}
    --provider:network_type {{ public_network_type }} \
    {% if public_segmentation_id != '' %}
    --provider:segmentation_id {{ public_segmentation_id }} \
{% endif %}
    --provider:physical_network {{ public_physical_network }}
{% endif %}

neutron subnet-create --name ext-subnet \
    --allocation-pool \
    start={{ public_net_pool_start }},end={{ public_net_pool_end }} \
    --disable-dhcp \
    --gateway {{ public_net_gateway }} \
{{ public_net_name }} {{ floating_ip_cidr }}

{% else %}

for i in $(openstack floating ip list -c ID -f value)
do
    openstack floating ip unset --port $i
    openstack floating ip delete $i
done
for i in $(openstack router list -c ID -f value); do openstack router unset --external-gateway $i; done
for r in $(openstack router list -c ID -f value); do
    for p in $(openstack port list --router $r -c ID -f value); do
        openstack router remove subnet $r $p || true
    done
done
for i in $(openstack router list -c ID -f value); do openstack router delete $i; done
for i in $(openstack port list -c ID -c Name -f value | sed -e '/octavia/d' | cut -f 1 -d ' '); do openstack port delete $i; done
for i in $(openstack network list -c ID -c Name -f value | sed -e '/lb-mgmt-net/d' | cut -f 1 -d ' '); do openstack network delete $i; done

{% if ironic_overcloud|default(false)|bool %}

curl https://tarballs.opendev.org/openstack/ironic-python-agent/tinyipa/tinyipa-stable-pike.tar.gz | tar -C ~ -xzf -
IPAKERNEL=$(openstack image create --public --container-format aki --disk-format aki --file ~/tinyipa-stable-pike.vmlinuz deploy-kernel -f value -c id)
IPARD=$(openstack image create --public --container-format ari --disk-format ari --file ~/tinyipa-stable-pike.gz deploy-ramdisk -f value -c id)

openstack image create --public --container-format bare --disk-format qcow2 --file {{ tempest_test_image_path }} cirros

openstack network create --share --provider-network-type flat --provider-physical-network datacentre --external provisioning
openstack subnet create --network provisioning --subnet-range 192.168.24.0/24 --gateway 192.168.24.40 --allocation-pool start=192.168.24.41,end=192.168.24.100 provisioning-subnet

NODEID=$(openstack baremetal node create --driver ipmi --driver-info ipmi_username=admin --driver-info ipmi_password=password --driver-info ipmi_address=127.0.0.1 --driver-info ipmi_port=1161 --resource-class baremetal --name node1 -f value -c uuid)
# TODO: MAC shouldn't be hardcoded
openstack baremetal port create --node $NODEID --local-link-connection switch_info=net-ans-br --local-link-connection port_id=ovs-node1i1 --local-link-connection switch_id=00:00:00:00:00:00 66:0d:1d:d8:0b:11
openstack baremetal node set --property memory_mb=4096 --property cpus=4 --property local_gb=10 --property cpu_arch=x86_64 $NODEID

openstack baremetal node manage $NODEID --wait
openstack baremetal node set $NODEID --driver-info deploy_kernel=$IPAKERNEL --driver-info deploy_ramdisk=$IPARD
openstack baremetal node provide $NODEID --wait

NODEID=$(openstack baremetal node create --driver ipmi --driver-info ipmi_username=admin --driver-info ipmi_password=password --driver-info ipmi_address=127.0.0.1 --driver-info ipmi_port=1162 --resource-class baremetal --name node2 -f value -c uuid)
# TODO: MAC shouldn't be hardcoded
openstack baremetal port create --node $NODEID --local-link-connection switch_info=net-ans-br --local-link-connection port_id=ovs-node2i1 --local-link-connection switch_id=00:00:00:00:00:00 66:0d:1d:d8:0b:22
openstack baremetal node set --property memory_mb=4096 --property cpus=4 --property local_gb=10 --property cpu_arch=x86_64 $NODEID

openstack baremetal node manage $NODEID --wait
openstack baremetal node set $NODEID --driver-info deploy_kernel=$IPAKERNEL --driver-info deploy_ramdisk=$IPARD
openstack baremetal node provide $NODEID --wait

{% else %}

openstack network create {{ public_net_name }} --external \
{% if public_physical_network != '' %}
    --provider-network-type {{ public_network_type }} \
    {% if public_segmentation_id != '' %}
    --provider-segment {{ public_segmentation_id }} \
{% endif %}
    --provider-physical-network {{ public_physical_network }}
{% endif %}

openstack subnet create ext-subnet \
    --allocation-pool \
    start={{ public_net_pool_start }},end={{ public_net_pool_end }} \
    --no-dhcp \
    --gateway {{ public_net_gateway }} \
    --network {{ public_net_name }} \
    --subnet-range {{ floating_ip_cidr }}
{% endif %}
{% endif %}
